package com.bete.updata;

import static com.bete.updata.UpdateConstant.DOWNLOAD_PATH;
import static com.bete.updata.UpdateConstant.DOWNLOAD_PATH_ENDING;
import static com.bete.updata.UpdateConstant.HTTPAPKURL;
import static com.bete.updata.UpdateConstant.SAVE_DOWNLOAD_PATH;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import me.jessyan.autosize.utils.LogUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class DownLoadActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(0, 0);
        setContentView(R.layout.activity_updata);
        // 需要传递的数据
        // 保存路径
        // 下载路径
        String downloadPath = getIntent().getStringExtra(DOWNLOAD_PATH);
        String saveDownloadPath = getIntent().getStringExtra(SAVE_DOWNLOAD_PATH);
        String download_path_ending = getIntent().getStringExtra(DOWNLOAD_PATH_ENDING);

        LogUtils.d(" downloadPath value is : " + downloadPath + ", saveDownloadPath : " + saveDownloadPath);
        // 下载文件
        OkhttpDonloadUtil.getInstance().downloadFile(downloadPath, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                LogUtils.e(e.toString());

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                InputStream is = null;
                byte[] buf = new byte[2048];
                int len = 0;
                FileOutputStream fos = null;
                //储存下载文件的目录
                File file = new File(saveDownloadPath, "temp." + download_path_ending);
                File dir = file.getParentFile();
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                try {
                    is = response.body().byteStream();
                    long total = response.body().contentLength();
                    fos = new FileOutputStream(file);
                    long sum = 0;
                    while ((len = is.read(buf)) != -1) {
                        fos.write(buf, 0, len);
                        sum += len;
                        int progress = (int) (sum * 1.0f / total * 100);
                        //下载中更新进度条
                        //  listener.onDownloading(progress);
                    }
                    fos.flush();
                    //下载完成
                    //   listener.onDownloadSuccess(file);
                    LogUtils.d("文件下载完成！");


                } catch (Exception e) {
                    //  listener.onDownloadFailed(e);

                    LogUtils.e(e.toString());
                } finally {

                    try {
                        if (is != null) {
                            is.close();
                        }
                        if (fos != null) {
                            fos.close();
                        }
                    } catch (IOException e) {
                        LogUtils.e(e.toString());
                    }

                }
            }
        });

    }
}
